package utils;

import index.Document;
import index.Term;

import java.io.File;

public class Util
{
    private static String  LineSeparator;
    private static String  htmlParserIgnorePattern;
    private static File    ResultFile;
    private static File    MapFile;
    private static File    wordListFile;
    private static File    invertDocumentFile;
    private static File    pageRankFile;
    private static String  Home;
    private static double  DefaultTermWeight;
    private static double  MaxPageTermCount;
    private static boolean logError;
    private static boolean logInfo;
    /**
     * something can only be done in the query mode, while others can only be
     * done in the non-query mode
     */
    private static boolean isQueryMode;

    static
    {
        LineSeparator = System.getProperty("line.separator");
        ResultFile = new File("result.txt");
        MapFile = new File("cache/map.se");
        wordListFile = new File("wordlist.se");
        invertDocumentFile = new File("invert_document.se");
        pageRankFile = new File("pagerank.se");
        Home = System.getProperty("user.dir");
        htmlParserIgnorePattern = "(&#[\\d]+;)|[\\|\u3000]";
        DefaultTermWeight = 20;
        MaxPageTermCount = 20000;
        logError = false;
        logInfo = false;
        isQueryMode = false;
    }

    /**
     * @return the lineSeparator
     */
    public static String getLineSeparator()
    {
        return LineSeparator;
    }

    /**
     * @return the htmlParserIgnorePattern
     */
    public static String getHtmlParserIgnorePattern()
    {
        return htmlParserIgnorePattern;
    }

    /**
     * @return the resultFile
     */
    public static File getResultFile()
    {
        return ResultFile;
    }

    /**
     * @return the mapFile
     */
    public static File getMapFile()
    {
        return MapFile;
    }

    /**
     * @return the wordListFile
     */
    public static File getWordListFile()
    {
        return wordListFile;
    }

    /**
     * @return the invertDocumentFile
     */
    public static File getInvertDocumentFile()
    {
        return invertDocumentFile;
    }

    /**
     * @return the pageRankFile
     */
    public static File getPageRankFile()
    {
        return pageRankFile;
    }

    /**
     * @return the home
     */
    public static String getHome()
    {
        return Home;
    }

    /**
     * @return the defaultTermWeight
     */
    public static double getDefaultTermWeight()
    {
        return DefaultTermWeight;
    }

    /**
     * @return the maxPageTermCount
     */
    public static double getMaxPageTermCount()
    {
        return MaxPageTermCount;
    }

    /**
     * @return the logError
     */
    public static boolean isLogError()
    {
        return logError;
    }

    /**
     * @return the logInfo
     */
    public static boolean isLogInfo()
    {
        return logInfo;
    }

    /**
     * @return the isQueryMode
     */
    public static boolean isQueryMode()
    {
        return isQueryMode;
    }

    /**
     * @param defaultTermWeight
     *            the defaultTermWeight to set
     */
    public static void setDefaultTermWeight(double defaultTermWeight)
    {
        DefaultTermWeight = defaultTermWeight;
    }

    /**
     * @param maxPageTermCount
     *            the maxPageTermCount to set
     */
    public static void setMaxPageTermCount(double maxPageTermCount)
    {
        MaxPageTermCount = maxPageTermCount;
    }

    /**
     * @param logError
     *            the logError to set
     */
    public static void setLogError(boolean logError)
    {
        Util.logError = logError;
    }

    /**
     * @param logInfo
     *            the logInfo to set
     */
    public static void setLogInfo(boolean logInfo)
    {
        Util.logInfo = logInfo;
    }

    /**
     * @param isQueryMode
     *            the isQueryMode to set
     */
    public static void setQueryMode(boolean isQueryMode)
    {
        if (Util.isQueryMode != isQueryMode)
        {
            Term.clean();
            Document.clean();
            Util.isQueryMode = isQueryMode;
        }
    }

    /**
     * get a document from its id
     * 
     * @param id
     *            a id identify the document
     * @return return a {@code file} if the document exists, return null if the
     *         document doesn't exists
     */
    public static File getDocument(int id)
    {
        String path = Home + "/cache/" + id;
        File file = new File(path);
        if (file.exists())
            return file;
        else
            return null;
    }
}
